package com.intpro.demo.Controller;

import com.intpro.demo.DAO.AdminUser;
import com.intpro.demo.DAO.Application;
import com.intpro.demo.DAO.CommonRep;
import com.intpro.demo.DAO.Message;
import com.intpro.demo.DAO.Role;
import com.intpro.demo.Service.ApplyService;
import com.intpro.demo.Service.MessageSevice;
import com.intpro.demo.Utils.DateUtil;
import com.intpro.demo.Utils.MyAnnotations.Authorization;
import com.intpro.demo.Utils.MyAnnotations.CurrentUser;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("api/admin")
public class AdminController
{

	@Autowired
	private ApplyService applyService;
	@Autowired
	private MessageSevice messageSevice;

	@Authorization(role = Role.admin)
	@PostMapping(value = "findAllTodo")
	public CommonRep findAll(@CurrentUser AdminUser u)
	{
		CommonRep rep = new CommonRep();
		var data = applyService.findAllAppTodo();

		rep.data = data;
		return rep;
	}

	@Authorization(role = Role.admin)
	@PostMapping(value = "findById")
	public CommonRep findById(@RequestBody Map<String, String> req)
	{
		CommonRep rep = new CommonRep();
		long id = Long.parseLong(req.get("id"));
		rep.data = applyService.findByAid(id);
		return rep;
	}

	@Authorization(role = Role.admin)
	@PostMapping(value = "pass")
	public CommonRep pass(@RequestBody Map<String, String> req)
	{
		var id = Long.parseLong(req.get("id"));
		var pass = Boolean.valueOf(req.get("pass"));
		CommonRep rep = new CommonRep();
		var a = applyService.passApp(id, pass);

		Message m = new Message();
		m.fromid = 1;
		m.toid = a.uid;
		m.hsread = false;
		m.createtime = new Date();
		m.title = "申请结果";
		m.msg = "您的申请已通过";
		if (!pass)
		{
			m.msg = "您的申请没有通过";
		}
		messageSevice.addMsg(m);
		return rep;
	}

	@Authorization(role = Role.admin)
	@PostMapping(value = "check")
	public CommonRep checkAdmin(@CurrentUser AdminUser u)
	{
		return new CommonRep();
	}
}
